package display;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import book.Book;

import term.dept.Department;
import term.dept.Department.Code;
import term.dept.course.Course;
import term.dept.course.section.Instructor;
import term.dept.course.section.Section;

import Util.Util;
import adoption.Adoption;
import exception.InvalidNameException;

public class AdoptionThread extends Thread {
	private File f;
	private String deptName, dept, course, section, credits, courseName,
				   capacity, enrollment, inst, track, authorF, titleF,
				   authorM, titleM, isbnF, isbnM, ed, status, publisher,
				   notes, prevEnroll, qtyNeed, estBB, comtext,
				   newPrice, usedPrice, invNum, bcodeU, bcodeN;
	private int crs;
	private ReentrantReadWriteLock readLock = new ReentrantReadWriteLock();
	private InventoryData inv;
	
	public AdoptionThread(File fin, InventoryData data) {
		f = fin;
		inv = data;
	}
	
	@Override
	public void run() {
		parse();
	}

	/**
	 * Parses the input file and saves the adoptions in the InventoryData object.
	 * @param books
	 */
	private void parse() {
		Util.printStatusString("parsing");
		Scanner sc;
		try {
			//int count = 0;
			sc = new Scanner(new FileInputStream(f)).useDelimiter("\t|\n");
		
			while (sc.hasNext()) {
				
				readLine(sc);
				
				//TODO see TODO above.
				ArrayList<Thread> threads = new ArrayList<Thread>();
				
				Department d;
				Course c;
				Section s;
				Instructor in;
				Book b;
				
				//check dept
				Code dcode = Department.Code.getCode(dept);
				if (dcode == null)
					Util.printErrorString("Unknown Department Code: "+dept);
				
				d = inv.getCurTerm().getDept(dcode);
				
				//check course
				c = d.getCourse(Integer.valueOf(course));
				
				if (c == null)
					c = d.addCourse(Integer.valueOf(course),courseName);
				
				//check inst / section
				in = Instructor.get(inst);
				s = c.getSection(section);
				
				if (s == null)
					s = c.addSection(section, in);
				
				//check book
				
				if (isbnF.equalsIgnoreCase("LM"))
					b = Book.LM;
				else if (isbnF.equalsIgnoreCase("NT"))
					b = Book.NT;
				else if (isbnF.equalsIgnoreCase("NA"))
					b = Book.NA;
				else {
					b = inv.getBook(invNum);
					//System.out.println("book already exists");
					if (b == null)
						b = new Book(clean(authorF), clean(titleF), Long.valueOf(isbnF),clean(ed),clean(invNum));
						
						//id.setCount(f.getName(), ++count);
				}
				//add adoption
				
				if (d == null)
					System.out.println("null dept");
				if (c == null)
					System.out.println("null course");
				if (s == null)
					System.out.println("null section");
				if (b == null)
					System.out.println("null book");
				
				
				Adoption a = new Adoption(s,b,status);
				//adoptions.add(a);
				//System.out.println(a.toString());
				s.addAdoption(a);
				inv.addAdoption(a);
				inv.addBook(b);
				System.out.println("adopted book: "+b.getInv_num()+" for course: "+s.getSec_id());
			}
			
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidNameException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("parsing finished");
		
		//return adoptions;
	}

	private void readLine(Scanner sc) {
		deptName = sc.next();
		dept = sc.next();
		course = sc.next(); crs = Integer.valueOf(course);
		section = sc.next();
		credits = sc.next();
		courseName = sc.next();
		capacity = sc.next();
		enrollment = sc.next();
		inst = sc.next();
		track = sc.next();
		authorF = sc.next();
		titleF = sc.next();
		authorM = sc.next();
		titleM = sc.next();
		isbnF = sc.next();
		isbnM = sc.next();
		ed = sc.next();
		status = sc.next();
		publisher = sc.next();
		notes = sc.next();
		prevEnroll = sc.next();
		qtyNeed = sc.next();
		estBB = sc.next();
		comtext = sc.next();
		newPrice = sc.next();
		usedPrice = sc.next();
		invNum = sc.next();
		bcodeU = sc.next();
		bcodeN = sc.next();
	}
	
	/**
	 * Removes single quotes (') from the given string.
	 * @param s
	 * @return
	 */
	private String clean(String s) {
		return s.replace('\'', ' ').replace('\\',' ');
	}
	
	private void printLine() {
		System.out.print("AT: "+isbnF+", ");
		System.out.print("ATS: "+deptName+", ");
		System.out.print("MBSNO: "+dept+", ");
		System.out.print("MBSCK: "+course+", ");
		System.out.print("AUTHOR: "+section+", ");
		System.out.print("TITLE: "+credits+", ");
		System.out.print("ED: "+courseName+", ");
		System.out.print("TYPE: "+capacity+", ");
		System.out.print("PUB: "+enrollment+", ");
		System.out.print("SPUB: "+inst+", ");
		System.out.print("ISBN: "+track+", ");
		System.out.print("ISBNCK: "+authorF+", ");
		System.out.print("LIST: "+titleF+", ");
		System.out.print("LISTFLG: "+ed+", ");
		System.out.print("REFFLAG: "+status+", ");
		System.out.print("REF: "+publisher+", ");
		System.out.print("RELFL: "+notes+", ");
		System.out.print("ACT: "+prevEnroll+", ");
		System.out.print("NTXFL: "+qtyNeed+", ");
		System.out.print("COMMENT: "+estBB+", ");
		System.out.print("GUIDE: "+comtext+", ");
		System.out.print("EDSTATUS: "+newPrice+", ");
		System.out.print("TXTRD: "+usedPrice+", ");
		System.out.print("LMT: "+invNum+"\n");
	}
}
